import React, { useEffect } from 'react';
import { useParams } from 'react-router-dom';
import { useRequest } from 'ahooks';
import { getComment } from './server';

const Comment = () => {
  const { run, refresh, data, cancel } = useRequest((id, type) => getComment({ id, type }), {
    manual: true, // useRequest 不会默认执行，需要通过 run 来触发执行。
    // 请求之前触发
    onBefore: (params) => {
      console.log('onBefore', params);
    },
    // 请求成功触发
    onSuccess: (result, params) => {
      console.log(result, params);
    },
    // 请求失败触发
    onError: (error) => {
      console.error(error);
    },
    // 请求完成触发
    onFinally: (params, result, error) => {
      console.log('onFinally', params, result, error);
    },
  });
  const { id } = useParams();
  useEffect(() => {
    run(id, 2);
  }, []);

  return (
    <div>
      评论
      <button onClick={refresh}>刷新</button>
    </div>
  );
};

export default Comment;
